package com.youlai.boot.modules.content.controller;

import com.youlai.boot.modules.content.service.LzCommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.modules.content.model.form.LzCommentForm;
import com.youlai.boot.modules.content.model.query.LzCommentQuery;
import com.youlai.boot.modules.content.model.vo.LzCommentVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 评论前端控制层
 *
 * @author lz0816
 * @since 2025-03-06 16:49
 */
@Tag(name = "评论接口")
@RestController
@RequestMapping("/api/v1/lzComments")
@RequiredArgsConstructor
public class LzCommentController  {

    private final LzCommentService lzCommentService;

    @Operation(summary = "评论分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('content:lzComment:query')")
    public PageResult<LzCommentVO> getLzCommentPage(LzCommentQuery queryParams ) {
        IPage<LzCommentVO> result = lzCommentService.getLzCommentPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "博客评论分页列表")
    @GetMapping("/blogPage")
    public PageResult<LzCommentVO> getLzBlogCommentPage(LzCommentQuery queryParams ) {
        IPage<LzCommentVO> result = lzCommentService.getLzBlogCommentPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增评论")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('content:lzComment:add')")
    public Result<Void> saveLzComment(@RequestBody @Valid LzCommentForm formData ) {
        boolean result = lzCommentService.saveLzComment(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取评论表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('content:lzComment:edit')")
    public Result<LzCommentForm> getLzCommentForm(
        @Parameter(description = "评论ID") @PathVariable Long id
    ) {
        LzCommentForm formData = lzCommentService.getLzCommentFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改评论")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('content:lzComment:edit')")
    public Result<Void> updateLzComment(
            @Parameter(description = "评论ID") @PathVariable Long id,
            @RequestBody @Validated LzCommentForm formData
    ) {
        boolean result = lzCommentService.updateLzComment(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除评论")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('content:lzComment:delete')")
    public Result<Void> deleteLzComments(
        @Parameter(description = "评论ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = lzCommentService.deleteLzComments(ids);
        return Result.judge(result);
    }
}
